/** 进行ui宽高适配 如 道具栏 90*90（宽高一样的）  给的图是125 * 80  直接用肯定不行 宽大于高 就设置宽为90  然后计算相应的高的大小*/
export default class AspectRatioTool extends Laya.Script {
    /** 最大宽*/
    public maxWidth: number =90;
    /** 最大高*/
    public maxHeight: number = 90;

    private curImage: fairygui.GLoader
    private widthHeightRatio: number;

    public SetSprite(gimage: fairygui.GLoader, atlasName: string, spriteName: string): void {
        this.curImage = gimage;
        window.gameFacade.AtlasMng.SetSpriteByName(this.curImage, atlasName, spriteName);
        // this.DoAdapt();
    }

    /** 进行适配 最大*/
    public DoAdapt() {
        this.curImage.ensureSizeCorrect();
        let width = this.curImage.width;
        let heigth = this.curImage.height;
        if (width > heigth) {
            this.SetWidth(this.maxWidth);
        }
        else {
            this.SetHeight(this.maxHeight);
        }
    }
    public SetWidth(width: number) {
        this.widthHeightRatio = this.curImage.width / this.curImage.height;
        let targetHeight = width / this.widthHeightRatio;
        this.curImage.setXY(width, targetHeight);
    }

    public SetHeight(height: number) {
        this.widthHeightRatio = this.curImage.width / this.curImage.height;
        let targetWidth = this.widthHeightRatio * height;
        this.curImage.setXY(targetWidth, height);
    }
}
